import java.util.HashMap;
import java.util.Map;

public class Solution403 {
    public boolean canCross(int[] stones) {
        if (stones[1]!=1){
            return false;
        }
        Map<Integer,Integer> map=new HashMap<>();
        for (int i=0;i<stones.length;i++){
            map.put(stones[i],i);
        }
        boolean[][] f=new boolean[stones.length][stones.length+1];
        f[1][1]=true;
        for (int i=2;i<stones.length;i++){
            for (int j=1;j<=i;j++){
                if (map.containsKey(stones[i]-j)) {
                    int p=map.get(stones[i]-j);
                    f[i][j]=f[p][j-1]|f[p][j]|f[p][j+1];
                }
            }
        }
        boolean flag=false;
        for (int i=0;i<stones.length;i++){
            flag|=f[stones.length-1][i];
        }
        return flag;
    }

    public static void main(String[] args) {
        System.out.println(new Solution403().canCross(new int[]{0,1,2,3,4,8,9,11}));
    }
}
